import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';
import 'package:mmkv/mmkv.dart';
import 'package:seedfour/app/constants.dart';
import 'package:seedfour/app/theme.dart';
import 'package:timeago/timeago.dart' as timeago;


import 'app/routes/app_routes.dart';
import 'app/time_format.dart';
import 'app/widget/tab_indicator.dart';


void main() async {
  await MMKV.initialize();
  timeago.setLocaleMessages('zh_CN', CustomZhMessages());

  // Fluwx().registerApi(
  //     appId: "wx4fd25f4cc731273b",
  //     universalLink: "https://www.seedfour.com/link/");

  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations(
    [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown],
  );

  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.dark, // 设置状态栏图标为黑色
    statusBarBrightness: Brightness.light, // 对于iOS，确保状态栏背景色为浅色
  ));

  // 初始化EasyLoading
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.ring
    ..loadingStyle = EasyLoadingStyle.custom
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.white
    ..backgroundColor = Colors.black.withOpacity(0.7)
    ..indicatorColor = Colors.white
    ..textColor = Colors.white
    ..maskColor = Colors.black.withOpacity(0.5)
    ..userInteractions = false
    ..dismissOnTap = false;

  runApp(const App());


}


class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    final colorScheme = ColorScheme.fromSeed(
      seedColor: const Color(0xFF495AFF),
    );

    return ScreenUtilInit(
      designSize: const Size(360, 690),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) {
        return GetMaterialApp(
          debugShowCheckedModeBanner: false,
          title: '师付用户端',
          localizationsDelegates: const [GlobalMaterialLocalizations.delegate],
          supportedLocales: const [Locale('zh', 'CN')],
          theme: ThemeData(
            useMaterial3: true,
            scaffoldBackgroundColor: AppColor.colorBackground,
            appBarTheme: AppBarTheme(
              elevation: 0,
              centerTitle: true,
              backgroundColor: Colors.white,
              surfaceTintColor: Colors.transparent,
              iconTheme: const IconThemeData(color: AppColor.colorFontBlack),
              titleTextStyle: TextStyle(
                color: AppColor.colorFontBlack,
                fontSize: kFontSizeMedium,
                fontWeight: AppFont.fontMedium,
              ),
            ),
            tabBarTheme: TabBarThemeData(
              labelStyle: TextStyle(
                color: AppColor.colorFontBlue,
                fontWeight: AppFont.fontMedium,
                fontSize: kFontSizeSmall,
              ),
              unselectedLabelStyle: TextStyle(
                color: AppColor.colorFontGrey,
                fontSize: kFontSizeSmall,
                fontWeight: AppFont.fontRegular,
              ),
              dividerColor: Colors.transparent,
              indicator: TCUnderlineTabIndicator(
                isRound: true,
                indicatorBottom: 6.w,
                indicatorWidth: 12.w,
                borderSide:
                    BorderSide(color: AppColor.colorFontBlue, width: 3.w),
              ),
            ),
            dividerTheme: DividerThemeData(
              space: 0.w,
              thickness: 0.5.w,
              color: AppColor.colorLineGrey,
            ),
            colorScheme: colorScheme,
            buttonTheme: ButtonThemeData(
              buttonColor: colorScheme.primary,
              textTheme: ButtonTextTheme.primary,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(kRadiusSmall),
              ),
            ),
            filledButtonTheme: FilledButtonThemeData(
              style: ButtonStyle(
                textStyle: WidgetStateProperty.all(
                  TextStyle(
                    fontSize: kFontSizeSmall,
                    fontWeight: AppFont.fontRegular,
                  ),
                ),
                backgroundColor: WidgetStateProperty.resolveWith((states) =>
                    states.contains(WidgetState.disabled)
                        ? AppColor.colorBtnGrey
                        : AppColor.colorFontBlue),
                foregroundColor: WidgetStateProperty.resolveWith((states) =>
                    states.contains(WidgetState.disabled)
                        ? AppColor.colorFontGrey
                        : Colors.white),
                shape: WidgetStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(kRadiusSmall),
                  ),
                ),
              ),
            ),
            elevatedButtonTheme: ElevatedButtonThemeData(
              style: ButtonStyle(
                elevation: WidgetStateProperty.all(4.w),
                backgroundColor: WidgetStateProperty.resolveWith((states) =>
                    states.contains(WidgetState.disabled)
                        ? Colors.grey[400]
                        : const Color(0xFF495AFF)),
                foregroundColor: WidgetStateProperty.all(Colors.white),
                shape: WidgetStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(kRadiusSmall),
                  ),
                ),
              ),
            ),
            switchTheme: SwitchThemeData(
              thumbColor: WidgetStateProperty.resolveWith((states) {
                return Colors.white;
              }),
              trackColor: WidgetStateProperty.resolveWith((states) {
                if (states.contains(WidgetState.selected)) {
                  return AppColor.colorFontBlue;
                }
                return const Color(0xFFE1E3E9);
              }),
              trackOutlineColor: WidgetStateProperty.resolveWith((states) {
                if (states.contains(WidgetState.selected)) {
                  return AppColor.colorFontBlue;
                }
                return const Color(0xFFE1E3E9);
              }),
            ),
          ),
          initialRoute: RouteGet.splash,
          getPages: RouteGet.getPages,
          builder: EasyLoading.init(),
        );
      },
    );
  }
}
